/* 1videoConference -- An open source video conferencing platform.
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Data;
using System.Diagnostics;
using System.IO;


namespace Adiance.VMukti.BAL
{   
    public static class UserInfo
    {        
        public static string UserName = "";
        public static string MyRole = "";
        public static string MeetingID = "";

        public static string CreUserName = "1videoConference";
        public static string CrePassword = "resiprocket";
        public static string CreMachName = "";

        public static string ConSMTPServer = "";
        public static string ConSMTPPort = "";
        public static string ConSMTPUserName = "";
        public static string ConSMTPPassword = "";
        public static string ConSMTPSSL = "";
        public static string ConAsteriskServer = "";
        public static string ConSiteURL = "http://vc.medinindia.com";
        public static string ConSitePath = "";
        public static string strMeetingMode = "";

        public static string strDBUser = "";
        public static string strDBPass = "";
        public static string strMachIP = "";


        public static bool hostflag = false;
        public static List<LoginUser> loginUsers = new List<LoginUser>();        

        static string MyURL = ""; 
        static string[] URLParams;
        static EventLog entLog;

        public static void Initialize()
        {
            try
            {
                entLog = new EventLog("Application", Environment.MachineName, "Adiance");
                entLog.WriteEntry("Initialize has been started");

                System.Reflection.Assembly ass = System.Reflection.Assembly.GetAssembly(typeof(UserInfo));
                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Zip Files")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Zip Files"));
                }
                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Temp")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Temp"));
                }
                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Presentation Layer")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Presentation Layer"));
                }
                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Data Layer")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Data Layer"));
                }
                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Business Layer")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Business Layer"));
                }
                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Image")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Image"));
                }
                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"SQL Scripts")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"SQL Scripts"));
                }

                Adiance.VMukti.DAL.Database db = new Adiance.VMukti.DAL.Database();

                DataSet ds = db.GetConfig();

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        switch (ds.Tables[0].Rows[i][0].ToString())
                        {
                            case "SMTPServer":
                                ConSMTPServer = ds.Tables[0].Rows[i][1].ToString();
                                break;

                            case "SMTPPort":
                                ConSMTPPort = ds.Tables[0].Rows[i][1].ToString();
                                break;

                            case "SMTPUserName":
                                ConSMTPUserName = ds.Tables[0].Rows[i][1].ToString();
                                break;

                            case "SMTPPassword":
                                ConSMTPPassword = ds.Tables[0].Rows[i][1].ToString();
                                break;

                            case "SMTPSSL":
                                ConSMTPSSL = ds.Tables[0].Rows[i][1].ToString();
                                break;

                            case "AsteriskServer":
                                ConAsteriskServer = ds.Tables[0].Rows[i][1].ToString();
                                break;

                            case "SiteURL":
                                ConSiteURL = ds.Tables[0].Rows[i][1].ToString();
                                break;

                            case "SitePath":
                                ConSitePath = ds.Tables[0].Rows[i][1].ToString();
                                strMachIP = ConSitePath.Replace("http://", "").Split('/')[0];
                                break;

                            case "DBUser":
                                strDBUser = ds.Tables[0].Rows[i][1].ToString();
                                break;

                            case "DBPass":
                                strDBPass = ds.Tables[0].Rows[i][1].ToString();
                                break;
                        }
                    }
                }
                entLog.Close();
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--UserInfo.cs--:--Initialize()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("Initialize=" + exp.Message);
                entLog.WriteEntry("Catch of Initialize" + exp.Message);
                entLog.Close();
            }
        }

        static string EnCodeString(string str)
        {
            try
            {
                int j = 0;

                Byte[] encodedBytes = ASCIIEncoding.ASCII.GetBytes(str);
                Byte[] ans = new Byte[encodedBytes.Length];
                foreach (Byte b in encodedBytes)
                {

                    int i = Convert.ToInt32(b);

                    if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122))
                    {
                        i += 4;
                        if ((i > 90 && i < 97) || (i > 122 && i <= 129))
                            i = i - 26;

                    }
                    else if (i >= 48 && i <= 57)
                    {
                        i += 7;
                        if (i > 57)
                            i = i - 10;
                    }
                    else
                    {
                        if (i == 61)
                            i = 44;
                        else if (i == 44)
                            i = 61;
                    }

                    ans[j++] = Convert.ToByte(i);
                }
                return ASCIIEncoding.ASCII.GetString(ans);
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--UserInfo.cs--:--Encodestring()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("EnCodeString=" + exp.Message);
                return null;
            }
        }

        static string DeCodeString(string str)
        {
            try
            {
                int j = 0;

                Byte[] encodedBytes = ASCIIEncoding.ASCII.GetBytes(str);
                Byte[] ans = new Byte[encodedBytes.Length];
                foreach (Byte b in encodedBytes)
                {

                    int i = Convert.ToInt32(b);

                    if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122))
                    {
                        i -= 4;
                        if ((i < 65 && i > 61) || (i > 90 && i < 97))
                            i = i + 26;

                    }
                    else if (i >= 48 && i <= 57)
                    {
                        i -= 7;
                        if (i < 48)
                            i = i + 10;
                    }

                    else
                    {
                        if (i == 61)
                            i = 44;
                        else if (i == 44)
                            i = 61;
                    }

                    ans[j++] = Convert.ToByte(i);
                }
                return ASCIIEncoding.ASCII.GetString(ans);
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--UserInfo.cs--:--Decodestring()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("DeCodeString=" + exp.Message);
                return null;
            }
        }
       
    }

    public class UserService
    {
        public string XmlFileName = "";
        public string ControlName = "";
        public List<string> ServiceName = new List<string>();
        public List<string> ServiceBinding = new List<string>();
        public List<string> BindingType = new List<string>();
        public List<string> ClassName = new List<string>();
        public List<string> Parameters = new List<string>();
        public List<string> ServerInterface = new List<string>();
        public List<string> ClientInterface = new List<string>();
        public List<string> ServerClass = new List<string>();
    }

    public class UriList
    {
        public string Uriname = "";
        public string Controlname = "";
        public string Servicename = "";
        public string MeetingID;

        public UriList(string uri, string ctrlname, string svcname, string meetingid)
        {
            this.Uriname = uri;
            this.Controlname = ctrlname;
            this.Servicename = svcname;
            this.MeetingID = meetingid;

        }
    }

    public static class UserServices
    {
        public static List<UserService> MyServices = new List<UserService>();

        public static List<UriList> ServiceURI = new List<UriList>();

        public static List<ThreadedServiceHoster> ServiceThreads = new List<ThreadedServiceHoster>();

        public static string[] GetUri(string meetingid)
        {
            try
            {
                //string uriaddress = null;
                List<string> UriArray = new List<string>();

                for (int k = 0; k < ServiceURI.Count; k++)
                {
                    EventLog entLog2 = new EventLog("Application", Environment.MachineName, "Adiance");
                    entLog2.WriteEntry("ServiceUri Count " + ServiceURI.Count);
                    entLog2.Close();
                    if (ServiceURI[k].MeetingID == meetingid)
                    {
                        UriArray.Add(ServiceURI[k].Controlname + "~" + ServiceURI[k].Servicename + "`" + ServiceURI[k].Uriname);
                    }
                }


                return UriArray.ToArray();
                //for (int k = 0; k < ServiceURI.Count; k++)
                //{
                //    if (controlname == ServiceURI[k].Controlname)
                //    {
                //        if (servicename == ServiceURI[k].Servicename)
                //        {
                //            if (meetingid == ServiceURI[k].MeetingID)
                //            {
                //                uriaddress = ServiceURI[k].Uriname;
                //             }
                //        }
                //    }
                //}
                //System.Windows.MessageBox.Show("UriAddress = " + uriaddress + "for ControlName=" + controlname);
                //return uriaddress;
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--UserInfo.cs--:--GetUri()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("GetUri=" + exp.Message);
                return null;
            }
        }
    }

    public class LoginUser
    {
        public string UserName = "";
        //public ICliLoginStatusCallback objLoginStatus = null;

        //public LoginUser(string uName, ICliLoginStatusCallback myChannel)
        //{
        //    UserName = uName;
        //    objLoginStatus = myChannel;
        //}
    }

}
